home *** CD-ROM | disk | FTP | other *** search
/ Super PC 33 / Super PC 33 (Shareware).iso / spc / sonido / timidity / source / mix.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-03  |  11.5 KB  |  571 lines

  1. /*
  2.  
  3.     TiMidity -- Experimental MIDI to WAVE converter
  4.     Copyright (C) 1995 Tuukka Toivonen <titoivon@snakemail.hut.fi>
  5.  
  6.     Suddenly, you realize that this program is free software; you get
  7.     an overwhelming urge to redistribute it and/or modify it under the
  8.     terms of the GNU General Public License as published by the Free
  9.     Software Foundation; either version 2 of the License, or (at your
  10.     option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received another copy of the GNU General Public
  18.     License along with this program; if not, write to the Free
  19.     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     I bet they'll be amazed.
  21.  
  22.     mix.c */
  23.  
  24. #include <math.h>
  25. #include <stdio.h>
  26. #include <malloc.h>
  27.  
  28. #include "config.h"
  29. #include "common.h"
  30. #ifdef __WIN32__
  31. #include "instrume.h"
  32. #else
  33. #include "instruments.h"
  34. #endif
  35. #include "playmidi.h"
  36. #include "output.h"
  37. #include "controls.h"
  38. #include "tables.h"
  39. #include "resample.h"
  40. #include "mix.h"
  41.  
  42. /* Returns 1 if envelope runs out */
  43. int recompute_envelope(int v)
  44. {
  45.   int stage;
  46.  
  47.   stage = voice[v].envelope_stage;
  48.  
  49.   if (stage>5)
  50.     {
  51.       /* Envelope ran out. */
  52.       int tmp=(voice[v].status == VOICE_DIE); /* Already displayed as dead */
  53.       voice[v].status = VOICE_FREE;
  54.       if(!tmp)
  55.     ctl->note(v);
  56.       return 1;
  57.     }
  58.  
  59.   if (voice[v].sample->modes & MODES_ENVELOPE)
  60.     {
  61.       if (voice[v].status==VOICE_ON || voice[v].status==VOICE_SUSTAINED)
  62.     {
  63.       if (stage>2)
  64.         {
  65.           /* Freeze envelope until note turns off. Trumpets want this. */
  66.           voice[v].envelope_increment=0;
  67.           return 0;
  68.         }
  69.     }
  70.     }
  71.   voice[v].envelope_stage=stage+1;
  72.  
  73.   if (voice[v].envelope_volume==voice[v].sample->envelope_offset[stage])
  74.     return recompute_envelope(v);
  75.   voice[v].envelope_target=voice[v].sample->envelope_offset[stage];
  76.   voice[v].envelope_increment = voice[v].sample->envelope_rate[stage];
  77.   if (voice[v].envelope_target<voice[v].envelope_volume)
  78.     voice[v].envelope_increment = -voice[v].envelope_increment;
  79.   return 0;
  80. }
  81.  
  82. void apply_envelope_to_amp(int v)
  83. {
  84.   float lamp=voice[v].left_amp, ramp;
  85.   int32 la,ra;
  86.   if (voice[v].panned == PANNED_MYSTERY)
  87.     {
  88.       ramp=voice[v].right_amp;
  89.       if (voice[v].tremolo_phase_increment)
  90.     {
  91.       lamp *= voice[v].tremolo_volume;
  92.       ramp *= voice[v].tremolo_volume;
  93.     }
  94.       if (voice[v].sample->modes & MODES_ENVELOPE)
  95.     {
  96.       lamp *= vol_table[voice[v].envelope_volume>>23];
  97.       ramp *= vol_table[voice[v].envelope_volume>>23];
  98.     }
  99.  
  100.       la = FSCALE(lamp,AMP_BITS);
  101.       
  102.       if (la>MAX_AMP_VALUE)
  103.     la=MAX_AMP_VALUE;
  104.  
  105.       ra = FSCALE(ramp,AMP_BITS);
  106.       if (ra>MAX_AMP_VALUE)
  107.     ra=MAX_AMP_VALUE;
  108.  
  109.       
  110.       voice[v].left_mix=FINAL_VOLUME(la);
  111.       voice[v].right_mix=FINAL_VOLUME(ra);
  112.     }
  113.   else
  114.     {
  115.       if (voice[v].tremolo_phase_increment)
  116.     lamp *= voice[v].tremolo_volume;
  117.       if (voice[v].sample->modes & MODES_ENVELOPE)
  118.     lamp *= vol_table[voice[v].envelope_volume>>23];
  119.  
  120.       la = FSCALE(lamp,AMP_BITS);
  121.  
  122.       if (la>MAX_AMP_VALUE)
  123.     la=MAX_AMP_VALUE;
  124.  
  125.       voice[v].left_mix=FINAL_VOLUME(la);
  126.     }
  127. }
  128.  
  129. static int update_envelope(int v)
  130. {
  131.   voice[v].envelope_volume += voice[v].envelope_increment;
  132.   /* Why is there no ^^ operator?? */
  133.   if (((voice[v].envelope_increment < 0) &&
  134.        (voice[v].envelope_volume <= voice[v].envelope_target)) ||
  135.       ((voice[v].envelope_increment > 0) &&
  136.        (voice[v].envelope_volume >= voice[v].envelope_target)))
  137.     {
  138.       voice[v].envelope_volume = voice[v].envelope_target;
  139.       if (recompute_envelope(v))
  140.     return 1;
  141.     }
  142.   return 0;
  143. }
  144.  
  145. static void update_tremolo(int v)
  146. {
  147.   int32 depth=voice[v].sample->tremolo_depth<<7;
  148.  
  149.   if (voice[v].tremolo_sweep)
  150.     {
  151.       /* Update sweep position */
  152.  
  153.       voice[v].tremolo_sweep_position += voice[v].tremolo_sweep;
  154.       if (voice[v].tremolo_sweep_position>=(1<<SWEEP_SHIFT))
  155.     voice[v].tremolo_sweep=0; /* Swept to max amplitude */
  156.       else
  157.     {
  158.       /* Need to adjust depth */
  159.       depth *= voice[v].tremolo_sweep_position;
  160.       depth >>= SWEEP_SHIFT;
  161.     }
  162.     }
  163.  
  164.   voice[v].tremolo_phase += voice[v].tremolo_phase_increment;
  165.  
  166.   /* if (voice[v].tremolo_phase >= (SINE_CYCLE_LENGTH<<RATE_SHIFT))
  167.      voice[v].tremolo_phase -= SINE_CYCLE_LENGTH<<RATE_SHIFT;  */
  168.  
  169.   voice[v].tremolo_volume = 
  170.     1.0 - FSCALENEG((sine(voice[v].tremolo_phase >> RATE_SHIFT) + 1.0)
  171.             * depth * TREMOLO_AMPLITUDE_TUNING,
  172.             17);
  173.  
  174.   /* I'm not sure about the +1.0 there -- it makes tremoloed voices'
  175.      volumes on average the lower the higher the tremolo amplitude. */
  176. }
  177.  
  178. /* Returns 1 if the note died */
  179. static int update_signal(int v)
  180. {
  181.   if (voice[v].envelope_increment && update_envelope(v))
  182.     return 1;
  183.  
  184.   if (voice[v].tremolo_phase_increment)
  185.     update_tremolo(v);
  186.  
  187.   apply_envelope_to_amp(v);
  188.   return 0;
  189. }
  190.  
  191. #ifdef LOOKUP_HACK
  192. #  define MIXATION(a)    *lp++ += mixup[(a<<8) | (uint8)s];
  193. #else
  194. #  define MIXATION(a)    *lp++ += (a)*s;
  195. #endif
  196.  
  197. static void mix_mystery_signal(sample_t *sp, int32 *lp, int v, int count)
  198. {
  199.   Voice *vp = voice + v;
  200.   final_volume_t 
  201.     left=vp->left_mix, 
  202.     right=vp->right_mix;
  203.   int cc;
  204.   sample_t s;
  205.  
  206.   if (!(cc = vp->control_counter))
  207.     {
  208.       cc = control_ratio;
  209.       if (update_signal(v))
  210.     return;    /* Envelope ran out */
  211.       left = vp->left_mix;
  212.       right = vp->right_mix;
  213.     }
  214.   
  215.   while (count)
  216.     if (cc < count)
  217.       {
  218.     count -= cc;
  219.     while (cc--)
  220.       {
  221.         s = *sp++;
  222.         MIXATION(left);
  223.         MIXATION(right);
  224.       }
  225.     cc = control_ratio;
  226.     if (update_signal(v))
  227.       return;    /* Envelope ran out */
  228.     left = vp->left_mix;
  229.     right = vp->right_mix;
  230.       }
  231.     else
  232.       {
  233.     vp->control_counter = cc - count;
  234.     while (count--)
  235.       {
  236.         s = *sp++;
  237.         MIXATION(left);
  238.         MIXATION(right);
  239.       }
  240.     return;
  241.       }
  242. }
  243.  
  244. static void mix_center_signal(sample_t *sp, int32 *lp, int v, int count)
  245. {
  246.   Voice *vp = voice + v;
  247.   final_volume_t 
  248.     left=vp->left_mix;
  249.   int cc;
  250.   sample_t s;
  251.  
  252.   if (!(cc = vp->control_counter))
  253.     {
  254.       cc = control_ratio;
  255.       if (update_signal(v))
  256.     return;    /* Envelope ran out */
  257.       left = vp->left_mix;
  258.     }
  259.   
  260.   while (count)
  261.     if (cc < count)
  262.       {
  263.     count -= cc;
  264.     while (cc--)
  265.       {
  266.         s = *sp++;
  267.         MIXATION(left);
  268.         MIXATION(left);
  269.       }
  270.     cc = control_ratio;
  271.     if (update_signal(v))
  272.       return;    /* Envelope ran out */
  273.     left = vp->left_mix;
  274.       }
  275.     else
  276.       {
  277.     vp->control_counter = cc - count;
  278.     while (count--)
  279.       {
  280.         s = *sp++;
  281.         MIXATION(left);
  282.         MIXATION(left);
  283.       }
  284.     return;
  285.       }
  286. }
  287.  
  288. static void mix_single_signal(sample_t *sp, int32 *lp, int v, int count)
  289. {
  290.   Voice *vp = voice + v;
  291.   final_volume_t 
  292.     left=vp->left_mix;
  293.   int cc;
  294.   sample_t s;
  295.   
  296.   if (!(cc = vp->control_counter))
  297.     {
  298.       cc = control_ratio;
  299.       if (update_signal(v))
  300.     return;    /* Envelope ran out */
  301.       left = vp->left_mix;
  302.     }
  303.   
  304.   while (count)
  305.     if (cc < count)
  306.       {
  307.     count -= cc;
  308.     while (cc--)
  309.       {
  310.         s = *sp++;
  311.         MIXATION(left);
  312.         lp++;
  313.       }
  314.     cc = control_ratio;
  315.     if (update_signal(v))
  316.       return;    /* Envelope ran out */
  317.     left = vp->left_mix;
  318.       }
  319.     else
  320.       {
  321.     vp->control_counter = cc - count;
  322.     while (count--)
  323.       {
  324.         s = *sp++;
  325.         MIXATION(left);
  326.         lp++;
  327.       }
  328.     return;
  329.       }
  330. }
  331.  
  332. static void mix_mono_signal(sample_t *sp, int32 *lp, int v, int count)
  333. {
  334.   Voice *vp = voice + v;
  335.   final_volume_t 
  336.     left=vp->left_mix;
  337.   int cc;
  338.   sample_t s;
  339.   
  340.   if (!(cc = vp->control_counter))
  341.     {
  342.       cc = control_ratio;
  343.       if (update_signal(v))
  344.     return;    /* Envelope ran out */
  345.       left = vp->left_mix;
  346.     }
  347.   
  348.   while (count)
  349.     if (cc < count)
  350.       {
  351.     count -= cc;
  352.     while (cc--)
  353.       {
  354.         s = *sp++;
  355.         MIXATION(left);
  356.       }
  357.     cc = control_ratio;
  358.     if (update_signal(v))
  359.       return;    /* Envelope ran out */
  360.     left = vp->left_mix;
  361.       }
  362.     else
  363.       {
  364.     vp->control_counter = cc - count;
  365.     while (count--)
  366.       {
  367.         s = *sp++;
  368.         MIXATION(left);
  369.       }
  370.     return;
  371.       }
  372. }
  373.  
  374. static void mix_mystery(sample_t *sp, int32 *lp, int v, int count)
  375. {
  376.   final_volume_t 
  377.     left=voice[v].left_mix, 
  378.     right=voice[v].right_mix;
  379.   sample_t s;
  380.   
  381.   while (count--)
  382.     {
  383.       s = *sp++;
  384.       MIXATION(left);
  385.       MIXATION(right);
  386.     }
  387. }
  388.  
  389. static void mix_center(sample_t *sp, int32 *lp, int v, int count)
  390. {
  391.   final_volume_t 
  392.     left=voice[v].left_mix;
  393.   sample_t s;
  394.   
  395.   while (count--)
  396.     {
  397.       s = *sp++;
  398.       MIXATION(left);
  399.       MIXATION(left);
  400.     }
  401. }
  402.  
  403. static void mix_single(sample_t *sp, int32 *lp, int v, int count)
  404. {
  405.   final_volume_t 
  406.     left=voice[v].left_mix;
  407.   sample_t s;
  408.   
  409.   while (count--)
  410.     {
  411.       s = *sp++;
  412.       MIXATION(left);
  413.       lp++;
  414.     }
  415. }
  416.  
  417. static void mix_mono(sample_t *sp, int32 *lp, int v, int count)
  418. {
  419.   final_volume_t 
  420.     left=voice[v].left_mix;
  421.   sample_t s;
  422.   
  423.   while (count--)
  424.     {
  425.       s = *sp++;
  426.       MIXATION(left);
  427.     }
  428. }
  429.  
  430. /* Ramp a note out in c samples */
  431. static void ramp_out(sample_t *sp, int32 *lp, int v, int32 c)
  432. {
  433.  
  434.   /* should be final_volume_t, but uint8 gives trouble. */
  435.   int32 left, right, li, ri;
  436.  
  437.   sample_t s=0; /* silly warning about uninitialized s */
  438.  
  439.   left=voice[v].left_mix;
  440.   li=-(left/c);
  441.   if (!li) li=-1;
  442.  
  443.   /* printf("Ramping out: left=%d, c=%d, li=%d\n", left, c, li); */
  444.  
  445.   if (!(play_mode->encoding & PE_MONO))
  446.     {
  447.       if (voice[v].panned==PANNED_MYSTERY)
  448.     {
  449.       right=voice[v].right_mix;
  450.       ri=-(right/c);
  451.       while (c--)
  452.         {
  453.           left += li;
  454.           if (left<0)
  455.         left=0;
  456.           right += ri;
  457.           if (right<0)
  458.         right=0;
  459.           s=*sp++;
  460.           MIXATION(left);
  461.           MIXATION(right);
  462.         }
  463.     }
  464.       else if (voice[v].panned==PANNED_CENTER)
  465.     {
  466.       while (c--)
  467.         {
  468.           left += li;
  469.           if (left<0)
  470.         return;
  471.           s=*sp++;    
  472.           MIXATION(left);
  473.           MIXATION(left);
  474.         }
  475.     }
  476.       else if (voice[v].panned==PANNED_LEFT)
  477.     {
  478.       while (c--)
  479.         {
  480.           left += li;
  481.           if (left<0)
  482.         return;
  483.           s=*sp++;
  484.           MIXATION(left);
  485.           lp++;
  486.         }
  487.     }
  488.       else if (voice[v].panned==PANNED_RIGHT)
  489.     {
  490.       while (c--)
  491.         {
  492.           left += li;
  493.           if (left<0)
  494.         return;
  495.           s=*sp++;
  496.           lp++;
  497.           MIXATION(left);
  498.         }
  499.     }
  500.     }
  501.   else
  502.     {
  503.       /* Mono output.  */
  504.       while (c--)
  505.     {
  506.       left += li;
  507.       if (left<0)
  508.         return;
  509.       s=*sp++;
  510.       MIXATION(left);
  511.     }
  512.     }
  513. }
  514.  
  515.  
  516. /**************** interface function ******************/
  517.  
  518. void mix_voice(int32 *buf, int v, int32 c)
  519. {
  520.   Voice *vp=voice+v;
  521.   sample_t *sp;
  522.   if (vp->status==VOICE_DIE)
  523.     {
  524.       if (c>=MAX_DIE_TIME)
  525.     c=MAX_DIE_TIME;
  526.       sp=resample_voice(v, &c);
  527.       ramp_out(sp, buf, v, c);
  528.       vp->status=VOICE_FREE;
  529.     }
  530.   else
  531.     {
  532.       sp=resample_voice(v, &c);
  533.       if (play_mode->encoding & PE_MONO)
  534.     {
  535.       /* Mono output. */
  536.       if (vp->envelope_increment || vp->tremolo_phase_increment)
  537.         mix_mono_signal(sp, buf, v, c);
  538.       else
  539.         mix_mono(sp, buf, v, c);
  540.     }
  541.       else
  542.     {
  543.       if (vp->panned == PANNED_MYSTERY)
  544.         {
  545.           if (vp->envelope_increment || vp->tremolo_phase_increment)
  546.         mix_mystery_signal(sp, buf, v, c);
  547.           else
  548.         mix_mystery(sp, buf, v, c);
  549.         }
  550.       else if (vp->panned == PANNED_CENTER)
  551.         {
  552.           if (vp->envelope_increment || vp->tremolo_phase_increment)
  553.         mix_center_signal(sp, buf, v, c);
  554.           else
  555.         mix_center(sp, buf, v, c);
  556.         }
  557.       else
  558.         { 
  559.           /* It's either full left or full right. In either case,
  560.          every other sample is 0. Just get the offset right: */
  561.           if (vp->panned == PANNED_RIGHT) buf++;
  562.           
  563.           if (vp->envelope_increment || vp->tremolo_phase_increment)
  564.         mix_single_signal(sp, buf, v, c);
  565.           else 
  566.         mix_single(sp, buf, v, c);
  567.         }
  568.     }
  569.     }
  570. }
  571.